/*============================================================================*/
/*                        Tortoise Team                              		  */
/*============================================================================*/
/*                        OBJECT SPECIFICATION                                */
/*============================================================================*
* C Source:         %MAL_PWM.c%
* Instance:         RPL_1
* %version:         1 %
* %created_by:      Andres Torres Garcia %
* %date_created:    Sunday May 20 13:41:01 2012 %
*=============================================================================*/
/* DESCRIPTION : Source file for uC Abstraction layer for PWM module          */
/*============================================================================*/
/* FUNCTION COMMENT : Describes the functions for uC Abstraction Layer for PWM*/
/*	module																	  */
/*                                                                            */
/*============================================================================*/
/*                               OBJECT HISTORY                               */
/*============================================================================*/
/*  REVISION |   DATE      |                               |      AUTHOR      */
/*----------------------------------------------------------------------------*/
/*  1.0      | 25/05/2012  |                               | Andres Torres    */
/*	1.1		 | 15/09/2012  |							   | Andres Torres    */
/* Integration under Continuus CM                                             */
/*============================================================================*/

/* Includes */
/* -------- */
#include "MAL_EMIOS.h"
#include "MAL_IOAPI.h"
#include "stdtypedef.h"

/* Functions macros, constants, types and datas         */
/* ---------------------------------------------------- */
/* Functions macros */

/*==================================================*/ 
/* Definition of constants                          */
/*==================================================*/ 
/* BYTE constants */

/* WORD constants */

/* LONG and STRUCTURE constants */


/*======================================================*/ 
/* Definition of RAM variables                          */
/*======================================================*/ 
/* BYTE RAM variables */

/* WORD RAM variables */

/* LONG and STRUCTURE RAM variables */

/*======================================================*/ 
/* close variable declaration sections                  */
/*======================================================*/ 

/* Private defines */

/* Private functions prototypes */
/* ---------------------------- */


/* Exported functions prototypes */
/* ----------------------------- */
void vfnInitPWMChannel(T_UBYTE ub_dutyChannel, T_UWORD uw_dutyCycle, T_UBYTE ub_periodChannel, T_UWORD uw_period, T_UBYTE ub_counterBus, T_UBYTE ub_pinPcr);
void vfnInitPWM_Out_Of_Phase(T_UBYTE ubChannel, T_UWORD uw_registerA, T_UWORD uw_registerB, T_UBYTE ub_periodChannel, T_UWORD uw_period, T_UBYTE ub_counterBus, T_UBYTE ub_pinPcr);
void vfnInitPWM_Simple_Capture(T_UBYTE ubChannel, T_UBYTE ub_periodChannel, T_UWORD uw_period, T_UBYTE ub_counterBus, T_UBYTE ub_pinPcr, T_UBYTE ubPol);
void vfnSetDutyCycle(T_UBYTE ub_channel, T_UWORD uw_dutyCycle);
void vfnSetUpClock(void);

/* Inline functions */
/* ---------------- */

/* Private functions */
/* ----------------- */

/* Exported functions */
/* ------------------ */

/**************************************************************
 *  Name                 : 	vfnSetUpClock
 *  Description          :	Set up the clock of the PWM
 *  Parameters           :  [Input;Output; Input / output]
 *  Return               :	void
 *  Critical/explanation :  No
 **************************************************************/
void vfnSetUpClock(void)
{
	vfnSetup_Emios_0();
}

/**************************************************************
 *  Name                 : 	vfnInitPWMChannel
 *  Description          :	Init a PWM channel
 *  Parameters           :  [Input: ub_dutyChannel; uw_dutyCycle; ub_periodChannel; uw_period; ub_busSelected; ub_pinPcr, Output, Input / output]
 *  Return               :	void
 *  Critical/explanation :  No
 **************************************************************/
void vfnInitPWMChannel(T_UBYTE ub_dutyChannel, T_UWORD uw_dutyCycle, T_UBYTE ub_periodChannel, T_UWORD uw_period, T_UBYTE ub_counterBus, T_UBYTE ub_pinPcr)
{
	vfnInit_Emios_0_Mcb(ub_periodChannel, uw_period);//Init the counter.
	
	vfnInit_Emios_0_Opwm(ub_dutyChannel, 0, uw_dutyCycle, ub_counterBus);//Init the PWM with 100% of duty cycle.
	
	IO_SetPinFctn(ub_pinPcr,EMIOS_CHAN_OUT);//Set the pin as a Emios channel
}


/**************************************************************
 *  Name                 : 	vfnInitPWM_Out_Of_Phase
 *  Description          :	Set up the clock of the PWM
 *  Parameters           :  [Input: ub_dutyChannel, uw_registerA, uw_registerB, ub_periodChannel, uw_period, ub_counterBus, ub_pinPcr, ;Output; Input / output]
 *  Return               :	void
 *  Critical/explanation :  No
 **************************************************************/
void vfnInitPWM_Out_Of_Phase(T_UBYTE ubChannel, T_UWORD uw_registerA, T_UWORD uw_registerB, T_UBYTE ub_periodChannel, T_UWORD uw_period, T_UBYTE ub_counterBus, T_UBYTE ub_pinPcr)
{
	vfnInit_Emios_0_Mcb(ub_periodChannel, uw_period);//Init the counter.
	
	vfnInit_Emios_0_Opwm(ubChannel, uw_registerA, uw_registerB, ub_counterBus);
	
	IO_SetPinFctn(ub_pinPcr,EMIOS_CHAN_OUT);//Set the pin as a Emios channel
}

/**************************************************************
 *  Name                 : 	vfnInitPWM_Simple_Capture
 *  Description          :	Init the PWM as a simple capture.
 *  Parameters           :  [Input: ubChannel, ub_periodChannel, uw_period, ub_counterBus, ub_pinPcr, ubPol;Output; Input / output]
 *  Return               :	void
 *  Critical/explanation :  No
 **************************************************************/
void vfnInitPWM_Simple_Capture(T_UBYTE ubChannel, T_UBYTE ub_periodChannel, T_UWORD uw_period, T_UBYTE ub_counterBus, T_UBYTE ub_pinPcr, T_UBYTE ubPol)
{	
	vfnInit_Emios_0_Mcb(ub_periodChannel, uw_period);//Init the counter.
	
	vfnInit_Emios_0_Saic(ubChannel, ubPol, ub_counterBus);

	IO_SetPinFctn(ub_pinPcr, EMIOS_CHAN_IN);//Set the pin as a Emios channel
}

/**************************************************************
 *  Name                 : vfnSetDutyCycle
 *  Description          : Set a duty cycle for a channel
 *  Parameters           : [Input: ub_channel, uw_dutyCycle, Output, Input / output]
 *  Return               : void
 *  Critical/explanation : No
 **************************************************************/
void vfnSetDutyCycle(T_UBYTE ub_channel, T_UWORD uw_dutyCycle)
{
	vfnSet_Duty_Opwm( ub_channel,  uw_dutyCycle);//Set the duty cycle in the channel
}
